package com.dam.monngonmoingay;

import java.util.List;

import com.dam.monngonmoingay.MonAnAdapter.ViewHolder;
import com.dam.monngonmoingay.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListMonAnActivity extends Activity {
	MonNgonDB db;
	String[] danhsach = new String[1000];
	ListView lvDanhsach;
	ArrayAdapter<String> adapter;
	int id;
	MonAnAdapter adap;
	EditText TimKiemTextView;
	ImageButton backImageButton;
	ImageButton timKiemImageButton;
	Thread loadList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listmonan_layout);
		backImageButton = (ImageButton) findViewById(R.id.backImageButton);
		timKiemImageButton = (ImageButton) findViewById(R.id.timKiemImageButton);
		TimKiemTextView = (EditText) findViewById(R.id.timKiemTextView);
		
		Intent getid = getIntent();
		Bundle bundle = getid.getBundleExtra("id");
		id = bundle.getInt("id");

		final ProgressDialog dialog = ProgressDialog.show(this, "",
				"Đang tải dữ liệu ...", true);

		new Thread(new Runnable() {
			public void run() {
				lvDanhsach = (ListView) findViewById(R.id.monAnListView);
				final ArrayAdapter<MonAn> adapter = new MonAnAdapter(ListMonAnActivity.this, getMonNgonModel());
				ListMonAnActivity.this.runOnUiThread(new Runnable() {
					public void run() {
						lvDanhsach.setAdapter(adapter);
					}
				});

				lvDanhsach.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						ViewHolder holder = (ViewHolder) view.getTag();
						Bundle bundle = new Bundle();
						bundle.putLong("id", holder.id);
						bundle.putString("title", holder.title);
						bundle.putString("image", holder.image);
						bundle.putString("video", holder.video);
						bundle.putString("description", holder.description);
						bundle.putString("material", holder.material);
						bundle.putString("content", holder.content);
						bundle.putString("tips", holder.tips);
						bundle.putInt("like", holder.like);
						open(bundle);
					}
				});
				dialog.dismiss();
				ListMonAnActivity.this.runOnUiThread(new Runnable() {
					public void run() {

						if (lvDanhsach.getCount() > 0) {
						}
					}
				});

			}
		}).start();
		
		backImageButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
				startActivity(myIntent);
			}
		});
		
		timKiemImageButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MonNgonApplication app = (MonNgonApplication) getApplicationContext();
				id = 6;
				app.setLastCatalogId(6);
				app.setKeyWord("%"+TimKiemTextView.getText().toString()+"%");
				
				new Thread(new Runnable() {
					public void run() {
						lvDanhsach = (ListView) findViewById(R.id.monAnListView);
						final ArrayAdapter<MonAn> adapter = new MonAnAdapter(ListMonAnActivity.this, getMonNgonModel());
						ListMonAnActivity.this.runOnUiThread(new Runnable() {
							public void run() {
								lvDanhsach.setAdapter(adapter);
							}
						});

						lvDanhsach.setOnItemClickListener(new OnItemClickListener() {
							@Override
							public void onItemClick(AdapterView<?> parent, View view,
									int position, long id) {
								ViewHolder holder = (ViewHolder) view.getTag();
								Bundle bundle = new Bundle();
								bundle.putLong("id", holder.id);
								bundle.putString("title", holder.title);
								bundle.putString("image", holder.image);
								bundle.putString("video", holder.video);
								bundle.putString("description", holder.description);
								bundle.putString("material", holder.material);
								bundle.putString("content", holder.content);
								bundle.putString("tips", holder.tips);
								bundle.putInt("like", holder.like);
								open(bundle);
							}
						});
						dialog.dismiss();
						ListMonAnActivity.this.runOnUiThread(new Runnable() {
							public void run() {

								if (lvDanhsach.getCount() > 0) {
								}
							}
						});

					}
				}).start();
			}
		});
		
		TimKiemTextView.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
				// TODO Auto-generated method stub
				if(TimKiemTextView.getText().toString().equalsIgnoreCase("")){
					MonNgonApplication app = (MonNgonApplication) getApplicationContext();
					id = 6;
					app.setLastCatalogId(6);
					app.setKeyWord("%%");
					
					new Thread(new Runnable() {
						public void run() {
							lvDanhsach = (ListView) findViewById(R.id.monAnListView);
							final ArrayAdapter<MonAn> adapter = new MonAnAdapter(ListMonAnActivity.this, getMonNgonModel());
							ListMonAnActivity.this.runOnUiThread(new Runnable() {
								public void run() {
									lvDanhsach.setAdapter(adapter);
								}
							});

							lvDanhsach.setOnItemClickListener(new OnItemClickListener() {
								@Override
								public void onItemClick(AdapterView<?> parent, View view,
										int position, long id) {
									ViewHolder holder = (ViewHolder) view.getTag();
									Bundle bundle = new Bundle();
									bundle.putLong("id", holder.id);
									bundle.putString("title", holder.title);
									bundle.putString("image", holder.image);
									bundle.putString("video", holder.video);
									bundle.putString("description", holder.description);
									bundle.putString("material", holder.material);
									bundle.putString("content", holder.content);
									bundle.putString("tips", holder.tips);
									bundle.putInt("like", holder.like);
									open(bundle);
								}
							});
							dialog.dismiss();
							ListMonAnActivity.this.runOnUiThread(new Runnable() {
								public void run() {

									if (lvDanhsach.getCount() > 0) {
									}
								}
							});

						}
					}).start();
				}
				return false;
			}
		});
	}
	
	public List<MonAn> getMonNgonModel() {
		MonNgonDB monNgonDB = new MonNgonDB(this);
		List<MonAn> lists;
		if(id == 6){
			MonNgonApplication app = (MonNgonApplication) getApplicationContext();
			String name = app.getKeyWord();
			lists = monNgonDB.getListByName(name);
		}		
		else if(id!=5){
			lists= monNgonDB.getListById(id);
		}
		else{
			lists = monNgonDB.getListByLike();
		}
		return lists;
	}

	public void open(Bundle bundle) {
		Intent myIntent = new Intent(this, MonAnActivity.class);
		myIntent.putExtras(bundle);
		startActivity(myIntent);

	}

}